package id.net.iconpln.bb.server.dao.impl;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import oracle.jdbc.OracleTypes;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import id.net.iconpln.bb.server.dao.UserDao;
import id.net.iconpln.bb.server.model.persistence.MasterModul;
import id.net.iconpln.bb.server.model.persistence.ModelMasterBb;
import id.net.iconpln.bb.server.model.persistence.User;
import id.net.iconpln.bb.server.model.persistence.UserBb;
import id.net.iconpln.bb.server.util.SimpleMD5;

	@Repository("userDao")
	public class UserDaoHibernate implements UserDao {
	    private SessionFactory sessionFactory;

	    @Autowired
	    public void setSessionFactory(SessionFactory sessionFactory) {
	        this.sessionFactory = sessionFactory;
	    }

	    @Override
	    public List<User> findById(String name, String pass) {
//	        Query q = sessionFactory.getCurrentSession().createQuery("from User u where u.idUser = 'CMS' and u.passwd = 'CMS'");
	        SimpleMD5 simpleMd5 = new SimpleMD5();
	        String password = "";
	        try {
	             pass = simpleMd5.MD5(pass.trim());
	        } catch (NoSuchAlgorithmException ex) {
	            Logger.getLogger(UserDaoHibernate.class.getName()).log(Level.SEVERE, null, ex);
	        } catch (UnsupportedEncodingException ex) {
	            Logger.getLogger(UserDaoHibernate.class.getName()).log(Level.SEVERE, null, ex);
	        }
	        
	        List<User> user = sessionFactory.getCurrentSession().createQuery("from User u where u.idUser ='" + name.toUpperCase() + "' and u.passwd='" + pass + "'")
	                .list();
	        return user;
	    }

	    @Override
	    public void save(User user) {
	        sessionFactory.getCurrentSession().save(user);
	    }

	    @Override
	    public void delete(User user) {
	        throw new UnsupportedOperationException("Not supported yet.");
	    }

	    @Override
	    public Long count() {
	        throw new UnsupportedOperationException("Not supported yet.");
	    }
	    @Override
	    public List findAll() {
	        throw new UnsupportedOperationException("Not supported yet.");
	    }

	    @Override
	    public List<User> getUserByUser(String user) {
	        List<User> lUser = new ArrayList<User>();
	        System.out.println(user);
	        try{
	            lUser = sessionFactory.getCurrentSession().createQuery("from User u where u.idUser =?").setParameter(0, user).list();
	        }catch (Exception e){
	            System.out.println("dao" + e.getMessage());
	        }
	        return lUser;
	    }

		@Override
		public List<UserBb> getUserByLogin(String user, String Pass) {
			// TODO Auto-generated method stub
			List<UserBb> g = new ArrayList<UserBb>();
//			System.out.println("PACKAGE BBTRX.LOGINUSER");
			try {
				
				Connection con = sessionFactory.getCurrentSession().connection();
				String sql = null;
				sql = "{? = call BBTRX.LOGINUSER (?,?)}";
				System.out.println("PACKAGE BBTRX.LOGINUSER");	
				CallableStatement cst = con.prepareCall(sql);
				cst.setString(2, user);
				cst.setString(3, Pass);
				cst.registerOutParameter(1,OracleTypes.CURSOR);
				cst.execute();

				ResultSet rset = (ResultSet) cst.getObject(1);
				while (rset.next()) {
					UserBb u = new UserBb();
					u.setModule(rset.getString("MODULE").toString());
					u.setUser(rset.getString("USERNAME").toString());
					u.setNama_unit(rset.getString("NAMA_UNIT").toString());
					u.setId_unit(rset.getString("ID_UNIT").toString());
					u.setJenis_unit(rset.getString("JENIS_UNIT").toString());
					u.setLeveluser(rset.getString("LEVELUSER").toString());
					g.add(u);
				}

			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return g;
		}

	}

